package com.kly.sc.api.application.controller.im;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.service.user.GreetingService;
import com.kly.sc.api.application.service.utils.BaseComponent;
import com.kly.sc.api.application.utils.Result;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.user.service.UserMsgService;
import com.kly.user.service.WhiteListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/greeting")
@Api(value = "/api/greeting", tags = "快捷回复")
public class GreetingController extends BaseComponent {

    @Resource
    UserMsgService userMsgService;

    @Autowired
    private GreetingService greetingService;
    @Resource
    private WhiteListService whiteListService;

    @GetMapping("/flirting")
    @ApiOperation(value = "flirting消息", notes = "flirting消息")
    public Result<String> flirting() {
        String result = "";
        final String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        Response<List<String>> whiteList = whiteListService.userCodeList();
        boolean inWhiteList = CollectionUtils.isNotEmpty(whiteList.getData()) && whiteList.getData().contains(userCode);
        // 【meboo-v3.1.3】加白账号，IM私聊页内，不显示快捷话术一整条需求
        List<String> messageList = RpcUtils.getResult(userMsgService.getGreetingContent(inWhiteList ? "140" : "123", 1, getAppCode()));
        if (!messageList.isEmpty()) {
            result = messageList.get(0);
        }
        return Result.ok(result);
    }

    @GetMapping("/getMessage")
    @ApiOperation(value = "快速回复语句", notes = "快速回复语句")
    public Result<List<String>> getMessage() {
        // 【meboo-v3.1.3】加白账号，IM私聊页内，不显示快捷话术一整条需求
        final String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        Response<List<String>> whiteList = whiteListService.userCodeList();
        boolean inWhiteList = CollectionUtils.isNotEmpty(whiteList.getData()) && whiteList.getData().contains(userCode);
        if (inWhiteList || super.showPPPV()) {
            return Result.ok(Collections.emptyList());
        }
        List<String> messageList = RpcUtils.getResult(userMsgService.getGreetingContent("124", 2, getAppCode()));
        return Result.ok(messageList);
    }

    @GetMapping("/merryChristmas")
    @ApiOperation(value = "圣诞节祝福im消息", notes = "圣诞节祝福im消息")
    public Result merryChristmas(@RequestParam @ApiParam("userCode") String userCode) {

        greetingService.merryChristmas(userCode);
        return Result.ok();
    }

}
